import { defineStore } from "pinia";
import { store } from "@/store";
import type { routerType, viceMenusTypes } from "./types";
import { router } from "@/router";
import { subjectModules } from "@/utils/constant";

export const useRouterStore = defineStore({
  id: "pure-router",
  state: (): routerType => ({
    subject: "ALL", // ALL WATER ELECTRICTITY GAS MACHINE FAB
    pmsClass: "",
    subjectChinses: "all",
    showViceMenus: true,
    activeViceMenu: "",
    currentViceMenus: [],
    bottomFooterHeight: 0,
    leftWidth: 0
  }),
  getters: {
    getSubject() {
      return this.subject;
    },
    getPmsClass() {
      return this.pmsClass;
    },
    getShowViceMenus() {
      return this.showViceMenus;
    },
    getActiveViceMenu() {
      return this.activeViceMenu;
    },
    getCurrentViceMenus() {
      return this.currentViceMenus;
    },
    getBottomFooterHeight() {
      return this.bottomFooterHeight;
    },
    getLeftWidth() {
      return this.leftWidth;
    },
    getClassName() {
      const classListStr = localStorage.getItem("classList");
      if (classListStr) {
        let classList = JSON.parse(classListStr);
        classList = classList.filter(item => !!item);
        if (classList?.length === 1) {
          return classList[0]?.code;
        } else {
          return "";
        }
      }
      return "";
    },
    getClassList() {
      const classListStr = localStorage.getItem("classList");
      if (classListStr) {
        let classList = JSON.parse(classListStr);
        classList = classList.filter(item => !!item);
        return classList;
      }
      return [];
    }
  },
  actions: {
    SET_SUBJECT_(s?: string) {
      this.subject = s;
    },
    SET_SUBJECT() {
      const { currentRoute } = router;
      const { fullPath } = currentRoute.value;
      if (fullPath) {
        const pathArr = fullPath.split("/");
        pathArr.shift();
        if (!pathArr.length) return;
        const currentSubjectType = pathArr[0];
        subjectModules.some(item => {
          if (item.type === currentSubjectType) {
            this.subject = item.subject;
            this.subjectChinses = item.label;
            this.pmsClass = item.pmsClass;
            localStorage.setItem("router_subject", this.subject);
          }
        });
      }
    },
    SET_SHOW_VICE_MENUS(flag: boolean) {
      this.showViceMenus = flag;
    },
    RESET_SHOW_VICE_MENUS() {
      this.showViceMenus = true;
    },
    SET_CURRENT_VICE_MENUS(ar?: Array<viceMenusTypes> | undefined | null) {
      this.RESET_CURRENT_VICE_MENUS();
      ar?.length && this.currentViceMenus.push(...ar);
    },
    RESET_CURRENT_VICE_MENUS() {
      this.currentViceMenus.splice(0);
    },
    SET_ACTIVE_VICE_MENU(m: string) {
      this.activeViceMenu = m;
    },
    RESET_ACTIVE_VICE_MENU() {
      this.activeViceMenu = this.currentViceMenus?.length ? this.currentViceMenus[0] : "";
    },
    SET_BOTTOM_FOOTER_HEIGHT(h: number) {
      this.bottomFooterHeight = h;
    },
    SET_LEFT_WIDTH(w: number) {
      this.leftWidth = w;
    }
  }
});

export function useRouterStoreHook() {
  return useRouterStore(store);
}
